package com.innovation.sys.common.vo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.innovation.sys.common.enums.IRes;

@SuppressWarnings({ "serial", "rawtypes", "unchecked" })
public class Ret extends HashMap implements Serializable{

	private static final String CODE = "code";
	private static final int CODE_OK_VALUE = 200;
	private static final int CODE_FAIL_VALUE = 500;
	private static final String CODE_MSG = "msg";
	private static final String CODE_OK_MSG = "操作成功";
	private static final String CODE_FAIL_MSG = "服务器异常";

	public Ret() {
	}

	public static Ret by(Object key, Object value) {
		return new Ret().set(key, value);
	}

	public static Ret create(Object key, Object value) {
		return new Ret().set(key, value);
	}

	public static Ret create() {
		return new Ret();
	}

	public static Ret ok() {
		return new Ret().setOk(null);
	}

	public static Ret ok(IRes res) {
		return new Ret().setOk(res);
	}

	public static Ret ok(Object key, Object value) {
		return ok().set(key, value);
	}

	public static Ret fail() {
		return new Ret().setFail(null);
	}

	public static Ret fail(IRes res) {
		return new Ret().setFail(res);
	}

	public static Ret fail(Object key, Object value) {
		return fail().set(key, value);
	}

	public Ret setOk(IRes res) {
		if (null == res) {
			super.put(CODE, CODE_OK_VALUE);
			super.put(CODE_MSG, CODE_OK_MSG);
			return this;
		}
		setStatusCode(res);
		return this;
	}

	private void setStatusCode(IRes res) {
		super.put(CODE, res.getCode());
		super.put(CODE_MSG, res.getMsg());
	}

	public Ret setFail(IRes res) {
		if (null == res) {
			super.put(CODE, CODE_FAIL_VALUE);
			super.put(CODE_MSG, CODE_FAIL_MSG);
			return this;
		}
		setStatusCode(res);
		return this;
	}

	public boolean isOk() {
		Integer state = (Integer) get(CODE);
		if (state.intValue() == CODE_OK_VALUE) {
			return true;
		}
		return false;
	}

	public boolean isFail() {
		Integer state = (Integer) get(CODE);
		if (state.intValue() != CODE_OK_VALUE) {
			return true;
		}
		return false;
	}

	public Ret set(Object key, Object value) {
		super.put(key, value);
		return this;
	}

	public Ret set(Map map) {
		super.putAll(map);
		return this;
	}

	public Ret set(Ret Res) {
		super.putAll(Res);
		return this;
	}

	public Ret delete(Object key) {
		super.remove(key);
		return this;
	}

	public <T> T getAs(Object key) {
		return (T) get(key);
	}

	public String getStr(Object key) {
		Object s = get(key);
		return s != null ? s.toString() : null;
	}

	public Integer getInt(Object key) {
		Number n = (Number) get(key);
		return n != null ? n.intValue() : null;
	}

	public Long getLong(Object key) {
		Number n = (Number) get(key);
		return n != null ? n.longValue() : null;
	}

	public Number getNumber(Object key) {
		return (Number) get(key);
	}

	public Boolean getBoolean(Object key) {
		return (Boolean) get(key);
	}

	public boolean notNull(Object key) {
		return get(key) != null;
	}

	public boolean isNull(Object key) {
		return get(key) == null;
	}

	public boolean isTrue(Object key) {
		Object value = get(key);
		return (value instanceof Boolean && ((Boolean) value == true));
	}

	public boolean isFalse(Object key) {
		Object value = get(key);
		return (value instanceof Boolean && ((Boolean) value == false));
	}

	public boolean equals(Object Res) {
		return Res instanceof Ret && super.equals(Res);
	}
}

